package com.rustic.server.controller.api;

import cn.hutool.core.collection.CollUtil;
import cn.xy.base.server.bean.IPage;
import cn.xy.base.server.utils.PageUtil;
import cn.xy.commons.annotation.ApiAuth;
import com.rustic.server.dto.InteractionDataDTO;
import com.rustic.server.po.UserBrowseLink;
import com.rustic.server.service.UserBrowseLinkService;
import com.rustic.server.vo.list.UserBrowseLinkWebListVO;
import com.rustic.server.vo.UserBrowseLinkVO;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.rustic.server.web.WebContext;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import cn.xy.commons.core.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 表[user_browse_link]对应的前台接口
 *
 * @author auto
 */

@Slf4j
@RestController
@RequestMapping("/api/userBrowseLink")
@Api(tags = {"用户浏览关联接口"})
public class UserBrowseLinkController {

    @Autowired
    private UserBrowseLinkService userBrowseLinkService;

    @ApiOperation(value = "用户浏览关联-列表")
    @GetMapping("/list")
    public Result<Page<UserBrowseLinkWebListVO>> list(IPage page) {
        LambdaQueryWrapper<UserBrowseLink> queryWrapper = new LambdaQueryWrapper<>();
        //FIXME 根据需要修改搜索条件
        Page<UserBrowseLink> pageData = userBrowseLinkService.page(page.toPage(), queryWrapper);
        List<UserBrowseLink> userBrowseLinkList = pageData.getRecords();
        if (CollUtil.isEmpty(userBrowseLinkList)) {
            return Result.data(new Page<>());
        }
        List<UserBrowseLinkWebListVO> results = userBrowseLinkService.toWebListVo(userBrowseLinkList);
        return Result.data(PageUtil.toPage(pageData, results));
    }

    @ApiOperation(value = "用户浏览关联-详情")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "用户浏览关联id"),
    })
    @GetMapping("/detail")
    public Result<UserBrowseLinkVO> detail(int id) {
        if (id <= 0) {
            return Result.error(UserBrowseLinkService.USER_BROWSE_LINK_NOT_EXIST);
        }
        UserBrowseLink userBrowseLink = userBrowseLinkService.getById(id);
        if (userBrowseLink == null) {
            return Result.error(UserBrowseLinkService.USER_BROWSE_LINK_NOT_EXIST);
        }
        UserBrowseLinkVO userBrowseLinkVO = new UserBrowseLinkVO(userBrowseLink);
        return Result.data(userBrowseLinkVO);
    }

    @ApiOperation(value = "用户浏览关联-添加")
    @PostMapping("/add")
    @ApiAuth(ignore = true)
    public Result<String> add(@RequestBody @Validated InteractionDataDTO interaction) {
        int userId = WebContext.getUserId();
        userBrowseLinkService.addBrowse(userId, interaction.getTargetId(), interaction.getType());
        return Result.ok(null);
    }

}
